<?php
require('imageClass.php');

// Get the start time
$starttime = microtime();
$startarray = explode(" ", $starttime);
$starttime = $startarray[1] + $startarray[0];
// End get start time 

echo "<h1>Captcha</h1>";
$path ="images/small/";
$image = new Image();
echo "<h3>The term to match is:     ";
echo $image->getMatchingTerm();
echo "</h3>\n";


echo "<form action='evaluation.php' name='captcha' method='post'>\n";
echo "<table><tr><td>";
displayImages($image,$path);
echo "</td></tr><tr><td align='right'>";
echo "<input type='submit' name='selected' value='PASSED' />";
echo "</td></tr>";
echo createHiddenForm($image->getImages());
echo embedStartTime($starttime);
echo embedMacthingTerm($image->getMatchingTerm());
echo "</form>\n";

// This is the end of the page

echo $page->getFooter();
//  Nothing after this


// Hide
function displayImages($image,$path){
  $imageTermPair = $image->getImages();
  $length = sizeof($imageTermPair);
  
  echo "<table>";
  echo "<tr>";
  for ($counter=0;$counter< $length;$counter++){
    echo "<td>\n";
    echo "<input type='image' src='$path";
    echo $imageTermPair[$counter]->getImage();
    echo "' alt='";
    echo $imageTermPair[$counter]->getTerm();
    echo "' name='selected' value='";
    echo $imageTermPair[$counter]->getTerm();
    echo "' border=0 /> \n";
    echo "</td>\n";

    // Gives the table a second row
    if($counter == (($length/2)-1)){
      echo "</tr><tr>\n";
    }

  }
  echo "</tr>\n";
  echo "</table>\n";
}

function createHiddenForm($imageTermPair){
  $total ="";

  $length = sizeof($imageTermPair);
  for($counter=0;$counter < $length;$counter++){
    $image = $imageTermPair[$counter]->getImage();
    $term = $imageTermPair[$counter]->getTerm();
    $total.= "<input type='hidden' value='$image'name='hiddenImage$counter'>\n";
    $total.= "<input type='hidden' value='$term' name='hiddenText$counter'>\n";
  
  }
  return $total;
}

function embedStartTime($starttime){
  return  "<input type='hidden' value='$starttime' name='starttime'>\n";
}
function embedMacthingTerm($term){
    return  "<input type='hidden' value='$term' name='matchterm'>\n";
}
?>
